package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加商品进入购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart) {
        //需要将当前登录的用户id赋值到 shoppingCart 实体中，通过BaseContext获得
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //添加到购物车的是菜品
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //将用户id也添加到查询条件中
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        if (shoppingCart.getDishId() != null) {
            queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //查询当前菜品或者套餐是否在购物车中
        //SQL:select * from shopping_cart where user_id = ? and dish_id/setmeal_id = ?
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);

        //如果已经存在，就在原来数量基础上加一
        if (cartServiceOne != null) {
            cartServiceOne.setNumber(cartServiceOne.getNumber() + 1);
            shoppingCartService.updateById(cartServiceOne);
        } else {
            //如果不存在，则添加到购物车，数量默认就是一
            shoppingCartService.save(shoppingCart);
        }
        return R.success("添加成功~~~");
    }

    /**
     * 查询用户的购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        //通过BaseContext获取登录用户的id
        Long userId = BaseContext.getCurrentId();
        //去查询购物车表内的数据
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        //执行查询
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }

    /**
     * 清空该用户的购物车记录
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        //通过条件删除数据
        shoppingCartService.remove(queryWrapper);
        return R.success("清空成功");
    }

    /**
     * 减少购物车商品数量
     * @param
     * @return
     */
    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart1, HttpServletRequest request) {
        //先查询是否再次减少会为0，为0则删除该记录
        Long dishId = shoppingCart1.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getDishId, dishId);
        queryWrapper.eq(ShoppingCart::getUserId,request.getSession().getAttribute("user"));
        //查询该id和useId的购物车记录
        ShoppingCart shoppingCart = shoppingCartService.getOne(queryWrapper);
        //原来该商品只有一个，减少即删除该条记录
        if (shoppingCart.getNumber() == 1) {
            shoppingCartService.removeById(shoppingCart.getId());
            return R.success("更新成功");
        } else {
            //对数量进行减一操作
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            shoppingCartService.updateById(shoppingCart);
        }
        return R.success("更新成功");
    }
}
